
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.pull.tablename;

import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;

import dbmanager.DBManager;
//import java.util.Vector;

//import pool.bean.Trans_property_details_Bean;
import pool.bean.Transaction_Bean;
import query.query.ParseQuery;

public class Transaction extends IntegrateSubPool
{	
	
	private String poolTablename="transaction";
	private String reflectSchemaProperty = "trans_property_details";
	private String tableObject;
	private Hashtable<Object,Object> transHash = new Hashtable<Object, Object>();	
		
	public boolean insert(Hashtable<Object,Object> productHashtable,Hashtable<Object,Object>moduleHashtable)
	{	
		productTableHash.putAll(productHashtable);
		ModuleHash.putAll(moduleHashtable);
		
		setTransactionHashtable(); //Field-Value....		
		request=poolTablename;
		setRequestParameter();
		
			/** Inserting transaction & Property...**/
		System.out.println(" Integration::transaction:: Insert Process... ");
		
		if(!isTransactionAvailable())
		{			
			String tableId=gin.getId(tableField);
			String nameId= gin.getId(tableName);
			String query="select mid from property_details where pid="+tableId+" and pv="+nameId;
			ResultSet rs= DBManager.getSelect(query);
			try {
				rs.next();
				tableObject=rs.getString(1);			
			} catch (Exception e) { }
			
			transHash.put("transaction_type",gin.getItem(tableObject));
		}
		else
		{
			//fetch tid & add tid to all properties hashtable
			transHash.put("id",tidValue);			
		}
		Transaction_Bean trans = new Transaction_Bean();
		trans.setReqParam(req);			
		//trans.setId(tidValue);
		trans.setProcess(process);			
		trans.setTable(poolTablename);			
		trans.setHashtable(transHash);			
		trans.insert();
		
		return true;
	}
	
	private void setTransactionHashtable()
	{		
		Hashtable<Object,Object> tempHash = new Hashtable<Object, Object>(); // Stores the module hash.
		tempHash.putAll(ModuleHash);
		
		try 
		{	
			Enumeration<Object> enumt;		
			{
				enumt= tempHash.keys();
				while(enumt.hasMoreElements())
				{
					String key = enumt.nextElement().toString();
					String field = tempHash.get(key).toString();				
						transHash.put(field,productTableHash.get(key));									
				}
			}					
		} 
		catch (Exception e)	{
			e.printStackTrace();
		}	
	}

	public boolean update(Hashtable<Object, Object> productHashtable,Hashtable<Object, Object> moduleHashtable) 
	{
		productTableHash.putAll(productHashtable);
		ModuleHash.putAll(moduleHashtable);
		
		setTransactionHashtable(); //Field-Value	
		
		request=poolTablename;
		
		setRequestParameter();
		
		Hashtable<Object,Object> oldHash = new Hashtable<Object, Object>();
				
 		/** Inserting transaction & Property...**/
		System.out.println(" Integration::transaction:: Update Process... ");
		
		if(!isTransactionAvailable())
		{
			getTid();
		}		
		 
		oldHash.put("id",tidValue);		
		transHash.put("id",tidValue);
		
		Transaction_Bean trans = new Transaction_Bean();
		trans.setReqParam(req);
		trans.setProcess(process);		
		trans.setTable(poolTablename);	
		trans.setOldHashtable(oldHash);
		trans.setNewHashtable(transHash);
		trans.update();
		return true;	
	}


	public boolean delete(Hashtable<Object, Object> productTableHash, Hashtable<Object, Object> moduleHash) 
	{
		System.out.println(" poolint.pull.tablename :: Integration::transaction:: Delete Process... ");
		
		
		Enumeration< Object > enumEle = oldColumnNameValue.elements();
		Hashtable<Object, Object> delHash = new Hashtable<Object, Object>();
		
		while( enumEle.hasMoreElements() )
		{
			String colVal = enumEle.nextElement().toString();
			delHash.put( "id", colVal );
		}
		//delHash.put("id", oldcolumnNameValue);
		
		setRequestParameter();
		
		Transaction_Bean trans = new Transaction_Bean();
		trans.setReqParam(req);
		trans.setProcess(process);		
		trans.setTable(poolTablename);	
		trans.setOldHashtable(delHash);		
		System.out.println(" \npoolint.pull.tablename :: Transaction delete message \n" + trans.delete());
		
		return true;
	}	
	
	
	private String getTid()
	{
		//String colName=getFieldName();		
		Hashtable<Object, Object> colName = new Hashtable<Object, Object>();
		colName = getFieldName();
		String query = "";		
		Hashtable<Object, Object> temp = new Hashtable<Object, Object>();
		temp.putAll(columnNameValue);
		
		Enumeration<Object> keys = colName.keys();
		while(keys.hasMoreElements())
		{
			String key = keys.nextElement().toString();
			if(temp.containsKey(key))
			{
				String value = temp.get(key).toString();
				temp.remove(key);
				temp.put(key, value);
			}				
		}
		
		tidValue = parseQuerry(temp,"id");
		
		
		
		/*String condition = new String();
		
		Enumeration< Object > enumVal = columnNameValue.elements();
		
		while( enumVal.hasMoreElements() )
		{
			String colVal = enumVal.nextElement().toString();
			String mastervalue = gin.getId(colVal);
			
			if( !mastervalue.equals( "0" ) )
			{				
				condition +=  " pid = " + gin.getId(colName) + " and pv = " + mastervalue;
			}
			else
			{
				condition +=  " pid = " + gin.getId(colName) + " and vt = " + mastervalue;					
			}
		}
		//String masterValue = gin.getId(oldcolumnNameValue);
		//if(!(masterValue.equals("0")||masterValue.equals("no$")))
			//query="select tid from "+reflectSchemaProperty+" where pid="+gin.getId(colName)+" and pv='"+masterValue+"'";
		//else
			//query="select tid from "+reflectSchemaProperty+" where pid="+gin.getId(colName)+" and vt='"+oldcolumnNameValue+"'";
		
		query = " select tid from " + reflectSchemaProperty + " where " + condition;
		
		ResultSet rs;			
		 rs= DBManager.getSelect(query);
		 
		 try {
			 rs.next();
			tidValue=rs.getString(1);
			rs.close();
		} catch (Exception e) {
			// TODO: handle exception
		}*/
				
		return tidValue;
	}
	
	private String parseQuerry(Hashtable<Object, Object> condition, String field) 
	{
		ParseQuery query = new ParseQuery();
		query.SetCondition(condition);
		query.SetTableName("transaction");
		Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
		fieldHash.put("field", field);		
		query.SetFieldList(fieldHash);
		query.processQuery();
		String[] tid = query.getbuffer();
		return tid[1];
	}
	
	public boolean isParentAvailable()
	{		
		boolean flag=false;
		
		ResultSet rs = getJoinResultSet();		
		try 
		{
			rs.next();
			String id=rs.getString(1);	
			flag=false;
			rs.close();
		}
		catch (Exception e)
		{
			try 
			{
				ParseQuery query = new ParseQuery();
				query.SetCondition(columnNameValue);
				query.SetTableName("transaction");
				Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
				fieldHash.put("field", "id");		
				query.SetFieldList(fieldHash);
				query.processQuery();
				String[] id = query.getbuffer();
				if(id.length >= 1)
					flag= true;		
				
				/*String fieldname=gin.getId(getFieldName());
				String  query;
				String condition = new String();
				
				Enumeration< Object > enumVal = columnNameValue.elements();
				
				while( enumVal.hasMoreElements() )
				{
					String value = gin.getId( enumVal.nextElement().toString() );
					if(!value.equals("0"))
					{				
						condition +=  "pid = " + fieldname + " and pv = " + value;
					}
					else
					{
						condition +=  "pid = " + fieldname + " and vt = " + value;
					}
				}*/
				
				//String value=gin.getId(columnValue);
				//if(!value.equals("0"))
				//{				
					//query="select tid from "+reflectSchemaProperty+" where pid="+fieldname+" and pv="+value;
				//}
				//else
				//{
					//query="select tid from "+reflectSchemaProperty+" where pid="+fieldname+" and vt="+columnValue;
				//}
				
				/*query = " select tid from " + reflectSchemaProperty + " where " + condition;
				rs= DBManager.getSelect(query);
				rs.next();
				String id=rs.getString(1);	
				flag= true;
				rs.close();*/
			} 
			catch (Exception e1)
			{
				flag= false;
			}					
		}
		return flag;
	}
}
